package constant.milk.periodapp.alarm;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import constant.milk.periodapp.bean.SengriData;
import constant.milk.periodapp.database.DBSelectQuery;
import constant.milk.periodapp.database.DbAdapter;
import constant.milk.periodapp.util.MilkShared;
import constant.milk.periodapp.util.MilkUtil;

public class MilkAlarm
{
	public MilkAlarm()
	{
	}
	
	public void checkAlarm(Context context, DbAdapter db)
	{
		MilkShared s = new MilkShared(context);
		int alarm = s.getAlarm();
		
		if (alarm == 0)
		{
			stopAlarm(context);
		}
		else
		{
			startAlarm(context, db);
		}
	}

	public void stopAlarm(Context context)
	{
		Intent intent1 = new Intent(context, AlarmBroadcast.class);
		PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent1,
				PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(sender);
		sender.cancel();
	}

	public void startAlarm(Context context, DbAdapter db)
	{
		Intent intent1 = new Intent(context, AlarmBroadcast.class);
		PendingIntent sender1 = PendingIntent.getBroadcast(context, 0, intent1,
				PendingIntent.FLAG_NO_CREATE);

		// 알람이 이미 실행되고 있으면 알람 취소
		if (sender1 != null)
		{
			AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			manager.cancel(sender1);
		}

		MilkShared s = new MilkShared(context);
		int alarm = s.getAlarm();

		if (alarm == 0)
		{
			return;
		}
		
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		ArrayList<SengriData> list = new ArrayList<SengriData>();
		list = select.getSengriList();
		
		if (list.size() < 1)
		{
			return;
		}
		
		// 오늘날짜
		Calendar cal1 = Calendar.getInstance();
		long mill1 = cal1.getTimeInMillis();
		
		// 생리 최근 날짜
		Calendar cal2 = Calendar.getInstance();
		SengriData data = select.getSengriMaxDate();
		cal2.set(data.getYear(), data.getMonth(), data.getDay(), 12, 0, 0);
		cal2.add(Calendar.DAY_OF_MONTH, -1);
		long mill2 = cal2.getTimeInMillis();
		
		int term = s.getSengriTerm();
		
		if (s.getSengriType() == 0)
		{
			term = s.getSengriTerm();
		}
		else
		{
			term = MilkUtil.getAveSengriTerm(context, select);
		}
		
		while (mill1 >= mill2)
		{
			cal2.add(Calendar.DAY_OF_MONTH, term);
			
			mill2 = cal2.getTimeInMillis();
		}
		
		long alarmMill = cal2.getTimeInMillis();
		
		Intent intent = new Intent(context, AlarmBroadcast.class);
		PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);

		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		manager.cancel(sender);
		
		int version = Build.VERSION.SDK_INT;
		
		if (version >= 19)
		{
			manager.setExact(AlarmManager.RTC_WAKEUP, alarmMill, sender);
		}
		else
		{
			manager.set(AlarmManager.RTC_WAKEUP, alarmMill, sender);
		}
	}
}
